package com.dartfar.workflow.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.action.BaseAction;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.util.PathUtil;
import com.dartfar.workflow.model.WorkflowDeployment;
import com.dartfar.workflow.service.WorkflowDeploymentService;

/**
 * 流程定义Action
 * @author 王琦
 *
 */
@Scope("prototype")
@Component("workflowDeploymentAction")
public class WorkflowDeploymentAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private WorkflowDeploymentService workflowDeploymentService;

	private int id;
	private String filePath;
	private String fileName;
	private String message;
	private int version;
	
	private WorkflowDeployment workflowDeployment = new WorkflowDeployment();
	
	
	public String show(){
		
		return "show";
	}
	
	public String test(){
		System.out.println("ssss: " + workflowDeployment.getFileName());
		message = "OK";
		return SUCCESS;
	}
	
	/**
	 * 获取流程定义部署信息
	 * @return JSON
	 */
	public String loadWorkflowDeploymentData(){
		
		DataGridVo<WorkflowDeployment> dataGridVos = new DataGridVo<WorkflowDeployment>();
		
		List<WorkflowDeployment> list = workflowDeploymentService.findWorkflowFile(pageNo, pageSize);
		Long count = workflowDeploymentService.getWorkflowFileCount();
		
		if(list == null){
			list = new ArrayList<WorkflowDeployment>();
		}
		
		dataGridVos.setTotal(count);
		dataGridVos.setRows(list);
		
		String jsonResult = JSONObject.fromObject(dataGridVos).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 流程部署
	 * @return JSON
	 */
	public String deployment(){
		try{
	
		System.out.println("filePath: " + PathUtil.getWorkflowFilesPath() + fileName);
		filePath = PathUtil.getWorkflowFilesPath() + fileName;
		if(filePath != null && !filePath.equals("")){
			File file = new File(filePath);
			if(file.exists()){
				workflowDeploymentService.deployment(fileName, filePath, id);
				message = "部署成功!";
			}else{				
				message = "文件不存在!";
			}
		}else{
			message = "部署失败!";
		}
		
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public WorkflowDeployment getWorkflowDeployment() {
		return workflowDeployment;
	}

	public void setWorkflowDeployment(WorkflowDeployment workflowDeployment) {
		this.workflowDeployment = workflowDeployment;
	}
	
	
}
